package com.lenovo.smarttraffic.Test;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.database.dao.DaoManager;
import com.lenovo.smarttraffic.database.dao.DaoManager_Need;
import com.lenovo.smarttraffic.database.dao.NeedtitleBean;
import com.lenovo.smarttraffic.database.dao.NeedtitleBeanDao;
import com.lenovo.smarttraffic.database.dao.TitleStrBean;
import com.lenovo.smarttraffic.database.dao.TitleStrBeanDao;

import java.util.ArrayList;
import java.util.List;

public class SqlTestActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_GreenDao_show;
    private TextView tv_GreenDao_show;
    private Button btn_GreenDao_delete;
    private TitleStrBeanDao titleStrBeanDao;
    private TitleStrBean titleStrBean1;
    private TitleStrBean titleStrBean2;
    private TitleStrBean titleStrBean3;
    private TitleStrBean titleStrBean4;
    private NeedtitleBeanDao needtitleBeanDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sql_test);
        initView();
        DaoManager.getInstance().init(this);
        titleStrBeanDao = DaoManager.getInstance().getDaoSession().getTitleStrBeanDao();
        DaoManager_Need.getmInstance_Need().init(this);
        needtitleBeanDao = DaoManager_Need.getmInstance_Need().getDaoSession_Need().getNeedtitleBeanDao();
//             实时路况
//             文化
//             二手车
//             违章资讯
//        健康 娱乐
//        财经 体育
//        教育 视频
//        旅游 游戏
//        军事 电影

    }

    private void initView() {
        btn_GreenDao_show = (Button) findViewById(R.id.btn_GreenDao_show);
        btn_GreenDao_show.setOnClickListener(this);
        tv_GreenDao_show = (TextView) findViewById(R.id.tv_GreenDao_show);
        tv_GreenDao_show.setOnClickListener(this);
        btn_GreenDao_delete = (Button) findViewById(R.id.btn_GreenDao_delete);
        btn_GreenDao_delete.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_GreenDao_show:
                titleStrBean1 = new TitleStrBean(null, " 推荐  ");
                titleStrBean2 = new TitleStrBean(null, " 热点  ");
                titleStrBean3 = new TitleStrBean(null, "科技");
                titleStrBean4 = new TitleStrBean(null, "汽车资讯");
                titleStrBeanDao.insert(titleStrBean1);
                titleStrBeanDao.insert(titleStrBean2);
                titleStrBeanDao.insert(titleStrBean3);
                titleStrBeanDao.insert(titleStrBean4);

                List<TitleStrBean> list = titleStrBeanDao.queryBuilder().list();
                Log.e("=========", "onClick: " + list.size());
                StringBuilder stringBuilder = new StringBuilder();
                for (int i = 0; i < list.size(); i++) {
                    stringBuilder.append(list.get(i).getTitle() + "\n");
                }
                tv_GreenDao_show.setText(stringBuilder.toString());
                Toast.makeText(this, "query", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_GreenDao_delete:
                titleStrBeanDao.deleteAll();
                needtitleBeanDao.deleteAll();
                List<TitleStrBean> list1 = titleStrBeanDao.queryBuilder().list();
                List<NeedtitleBean> list2 = needtitleBeanDao.queryBuilder().list();
                Log.e("=========", "onClick: " + list1.size());
                Log.e("=========", "onClick: " + list2.size());
                Toast.makeText(this, "delete", Toast.LENGTH_SHORT).show();
                break;
        }
    }


}
